﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using LikeUs.WebService.Client;
using LikeUs.WebService.Client.WSInfoClient;

namespace LikeUsWebSite
{
    public class LikeUsProvider : MembershipProvider
    {
        //Minimun password length
        private int m_minRequiredPasswordLength = 6;
        //Minium non-alphanumeric char required
        private int m_minRequiredNonAlphanumericCharacters = 0;
        //Enable - disable password retrieval
        private bool m_enablePasswordRetrieval = true;
        //Enable - disable password reseting
        private bool m_enablePasswordReset = false;
        //Require security question and answer (this, for instance, is a functionality which not many people use)
        private bool m_requiresQuestionAndAnswer = false;
        //Application name
        private string m_applicationName = "LikeUs";
        //Max number of failed password attempts before the account is blocked, and time to reset that counter
        private int m_maxInvalidPasswordAttempts = 3;
        private int m_passwordAttemptWindow = 10;
        //Require email to be unique 
        private bool m_requiresUniqueEmail = true;
        //Password format
        private MembershipPasswordFormat m_passwordFormat = new MembershipPasswordFormat();
        //Regular expression the password should match (empty for none)
        private string m_passwordStrengthRegularExpression = String.Empty;
        private MembershipUser memberUser;

        public override string ApplicationName
        {
            get
            {
                return m_applicationName;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username,string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            WSInfoProxy wsProxy = new WSInfoProxy();            
            UserData user = new UserData();
            user.UserName = username;
            user.Password = password;
            user.Name1 = " ";
            user.Image = " ";
            if (email == "" || email == null)
                user.Email = " ";
            else
                user.Email = email;
            wsProxy.CreateUser(user);
            if (user.UserID != -1)
                status = MembershipCreateStatus.Success;
            else
                status = MembershipCreateStatus.DuplicateUserName;
            MembershipUser memberUser = new MembershipUser("LikeUsProvider", username, providerUserKey, email, passwordQuestion, user.UserID.ToString(), isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
            return memberUser;
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get 
            { 
                return m_enablePasswordReset;
            }
        }

        public override bool EnablePasswordRetrieval
        {
            get 
            { 
                return m_enablePasswordRetrieval; 
            }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            return memberUser;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get 
            { 
                return m_maxInvalidPasswordAttempts; 
            }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get 
            { 
                return m_minRequiredNonAlphanumericCharacters; 
            }
        }

        public override int MinRequiredPasswordLength
        {
            get 
            { 
                return m_minRequiredPasswordLength; 
            }
        }

        public override int PasswordAttemptWindow
        {
            get 
            { 
                return m_passwordAttemptWindow; 
            }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get 
            { 
                return m_passwordFormat; 
            }
        }

        public override string PasswordStrengthRegularExpression
        {
            get 
            { 
                return m_passwordStrengthRegularExpression; 
            }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get 
            {
                return m_requiresQuestionAndAnswer; 
            }
        }

        public override bool RequiresUniqueEmail
        {
            get 
            { 
                return m_requiresUniqueEmail; 
            }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            WSInfoProxy wsProxy = new WSInfoProxy();
            int userid= wsProxy.ValidateUserAccount(username, password);
            memberUser = new MembershipUser("LikeUsProvider", username, null, null, null, userid.ToString(), true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
            return userid != -1;
        }
    }
}